package kata06;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Set;

public class Anagrams {

    Trie tire = new Trie();
    public List<String> readTxtFiles (String filePath) throws IOException {
        String file = this.getClass().getClassLoader().getResource(filePath).getFile();
        FileReader reader = new FileReader(file);
        BufferedReader br = new BufferedReader(reader);
        String strLine;
        List<String> infos = new ArrayList<>();
        while ((strLine = br.readLine()) != null) {
            infos.add(strLine);
        }
        return  infos;
    }

    public void insertWordsToTrie (List<String> words) {
        for (String item : words) {
            tire.insert(item);
        }
    }

    public boolean isWordExit (String word) {
        return tire.search(word);
    }

    public List<String> getSortWordsExit(Collection<String> words){
        List<String> result = new ArrayList<>();
        for (String word : words) {
            if(this.isWordExit(word)){
                result.add(word);
            }
        }
        return result;
    }

    public List<String> getALLWordsExist(List<String> words) {
        List<String> result = new ArrayList<>();
        Arrange arrange = new Arrange();
        for (String word : words) {
            List<String> allSortWord = arrange.getAllSort(word);
            List<String> allSortCorrentWord = this.getSortWordsExit(allSortWord) ;
            if(allSortCorrentWord.size() != 0 && allSortCorrentWord.size() >= result.size() ){
                result = allSortCorrentWord;
            }
        }
        return result;
    }
}